package de.fh.ka.as.car.hardware;

import java.util.HashMap;

import android.hardware.SensorManager;
import de.fh.ka.as.car.sensor.AndroidSensor;
import de.fh.ka.as.car.sensor.AndroidSensor.SensorType;
import de.fh.ka.as.car.sensor.Compass;
import de.fh.ka.as.car.sensor.Speedometer;

public class Car {
	/**
	 * Orientation to north pole in Degree
	 */
	private double orientation;
	private double speed;

	HashMap<SensorType, AndroidSensor> phoneSensors = new HashMap<AndroidSensor.SensorType, AndroidSensor>();

	public Car(SensorManager sensorManager) {
		phoneSensors.put(SensorType.Compass, new Compass(sensorManager));
		phoneSensors.put(SensorType.Speedometer, new Speedometer(sensorManager));
	}

	public void disableSensors() {
		for (AndroidSensor sensor : phoneSensors.values())
			sensor.disableSensors();
	}

	public void enableSensors() {
		for (AndroidSensor sensor : phoneSensors.values())
			sensor.enableSensors();
	}

	public void disableSensor(SensorType type) {
		phoneSensors.get(type).disableSensors();
	}

	public void enableSensor(SensorType type) {
		phoneSensors.get(type).enableSensors();
	}

	public double getOrientation() {
		if (phoneSensors.containsKey(SensorType.Compass))
			orientation = phoneSensors.get(SensorType.Compass).getSensorData()[0];
		return orientation;
	}

	public double getSpeed() {
		if (phoneSensors.containsKey(SensorType.Speedometer))
			speed = phoneSensors.get(SensorType.Speedometer).getSensorData()[0];
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
		if (phoneSensors.containsKey(SensorType.Speedometer))
			((Speedometer) phoneSensors.get(SensorType.Speedometer)).setCurrentSpeed(speed);
	}
}
